<template>
  <el-card style="margin-bottom:20px;">
    <div slot="header" class="clearfix">
      <span>About me</span>
    </div>

    <div class="user-profile">
      <div class="box-center">
        <pan-thumb :image="userInfo.photo" :height="'100px'" :width="'100px'" :hoverable="false">
          <div>你瞅啥 </div>
        </pan-thumb>
      </div>
      <div class="box-center">
        <div class="user-name text-center">{{ userInfo.name }}</div>
        <button v-if="loginId===userInfo.id" class="gz" @click="errorFollow()">关注</button>
        <button v-else-if="userInfo.isFollow" class="qg" @click="followUser(userInfo.id)">取关</button>
        <button v-else class="gz" @click="followUser(userInfo.id)">关注</button>
      </div>
      <div>
        <span>姓名 : {{ userInfo.name }}</span><br>
        <span>性别 : {{ userInfo.gender }}</span><br>
        <span>电话号 : {{ userInfo.phone }}</span><br>
        <span>住址 : {{ userInfo.address }}</span><br>
        <span>生日 : {{ userInfo.birthday | formatDate }}</span><br>
        粉丝数 :<a href="/#" style="display: inline;" @click="getFans()">  {{ userInfo.fans }} </a>
        关注数 : <a href="/#" style="display: inline;" @click="getFollow()"> {{ userInfo.follow }} </a>
        <el-button @click="backs()">返回</el-button>
      </div>
      <div />
      <div>
        <el-dialog title="TA的关注" :visible.sync="followVisible" width="40%" :close-on-click-modal="false">
          <el-table :data="follow">
            <el-table-column label="头像">
              <template slot-scope="scope">
                <img :src="scope.row.photo" alt="用户头像" style="width: 50px; height: 50px;">
              </template>
            </el-table-column>
            <el-table-column prop="name" label="姓名" />
            <el-table-column prop="gender" label="性别" />
            <el-table-column prop="address" label="住址" />
            <el-table-column label="生日" prop="birthday">
              <template slot-scope="scope">
                {{ scope.row.birthday | formatDate }}
              </template>
            </el-table-column>
          </el-table>
        </el-dialog>
        <el-dialog title="TA的粉丝" :visible.sync="fansVisible" width="40%" :close-on-click-modal="false">
          <el-table :data="fans">
            <el-table-column label="头像">
              <template slot-scope="scope">
                <img :src="scope.row.photo" alt="用户头像" style="width: 50px; height: 50px;">
              </template>
            </el-table-column>
            <el-table-column prop="name" label="姓名" />
            <el-table-column prop="gender" label="性别" />
            <el-table-column prop="address" label="住址" />
            <el-table-column label="生日" prop="birthday">
              <template slot-scope="scope">
                {{ scope.row.birthday | formatDate }}
              </template>
            </el-table-column>
          </el-table>
        </el-dialog>
      </div>
    </div>

  </el-card>
</template>

<script>
import PanThumb from '@/components/PanThumb/index.vue'
import { getAction, postAction, putAction } from '@/utils/xhs-ajax'
export default {
  components: { PanThumb },
  props: {
    user: {
      type: Object,
      default: () => {
        return {
          name: '',
          email: '',
          avatar: '',
          role: ''
        }
      }
    }
  },
  data() {
    return {
      loginId: this.$store.getters.userId,
      userId: this.$route.params.id,
      userInfo: [],
      sheng: '',
      shi: '',
      qu: '',
      followVisible: false,
      fansVisible: false,
      fromVisible: false,
      passVisible: false,
      province: [], // 省份
      city: [], // 城市
      prefecture: [], // 地区
      imgerUrl: '',
      fans: [],
      follow: []
    }
  },
  computed: {
    displayedImage() {
      return this.imageUrl || this.userInfo.photo
    }
  },
  created() {
    this.findUser()
  },
  methods: {
    backs() {
      this.$router.push('/blogManager')
    },
    // 关注帖主
    followUser(id) {
      postAction('/follow/followUser.do', { id: id }).then(resp => {
        this.userInfo.isFollow = !this.userInfo.isFollow
        this.findUser()
      })
    },
    errorFollow() {
      this.$message.error('自己不能关注自己！')
    },
    formatDate() {
      return formatDate
    },
    getFans() {
      this.fansVisible = true
      getAction('/follow/toCheckUserFocus.do?userId=' + this.userId).then(res => {
        this.fans = res.data
      })
    },
    getFollow() {
      this.followVisible = true
      getAction('/follow/toCheckPassiveFocus.do?userId=' + this.userId).then(res => {
        this.follow = res.data
      })
    },
    findUser() {
      getAction('/user/toCheckUser.do?userId=' + this.userId).then(res => {
        this.userInfo = res.data
        getAction('/user/findAddress.do?address=' + this.userInfo.address).then(res => {
          this.sheng = res.data[0]
          this.shi = res.data[1]
          this.qu = res.data[2]
          this.getProvince()
          this.getCity(this.sheng)
          this.getPrefecture(this.shi)
        })
      })
    },
    changeUserInfo() {
      this.fromVisible = true
    },
    changeUserPass() {
      this.passVisible = true
    },
    onsubmit(userInfo) {
      console.log(userInfo)
      postAction('/user/updateUser.do?sheng=' + this.sheng + '&shi=' + this.shi + '&qu=' + this.qu, userInfo).then(res => {
        this.findUser()
        this.fromVisible = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
a {
  color: #409EFF;
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}
.box-center {
  margin: 0 auto;
  display: table;
}

.text-muted {
  color: #777;
}

.user-profile {
  .user-name {
    font-weight: bold;
  }

  .box-center {
    padding-top: 10px;
  }

  .user-role {
    padding-top: 10px;
    font-weight: 400;
    font-size: 14px;
  }

  .box-social {
    padding-top: 30px;

    .el-table {
      border-top: 1px solid #dfe6ec;
    }
  }

  .user-follow {
    padding-top: 20px;
  }
}

.user-bio {
  margin-top: 20px;
  color: #606266;

  span {
    padding-left: 4px;
  }

  .user-bio-section {
    font-size: 14px;
    padding: 15px 0;

    .user-bio-section-header {
      border-bottom: 1px solid #dfe6ec;
      padding-bottom: 10px;
      margin-bottom: 10px;
      font-weight: bold;
    }
  }
}
.qg {
  margin-left: 10px;
  padding: 5px 10px;
  border: none;
  background-color: #409EFF;
  color: white;
  border-radius: 4px;
  cursor: pointer;
}

.gz {
  margin-left: 10px;
  padding: 5px 10px;
  border: none;
  background-color: #b3450e;
  color: white;
  border-radius: 4px;
  cursor: pointer;
}
</style>
